import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_shop/pages/category.dart';
import 'package:flutter_shop/pages/home.dart';
import 'package:flutter_shop/pages/me.dart';
import 'package:flutter_shop/pages/shopCart.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() {
    return _IndexPageState();
  }
}

class _IndexPageState extends State<IndexPage>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  PageController _pageController;

  final List<BottomNavigationBarItem> _list = [
    BottomNavigationBarItem(icon: Icon(CupertinoIcons.home), title: Text('首页')),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.search), title: Text('分类')),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.shopping_cart), title: Text('购物车')),
    BottomNavigationBarItem(
        icon: Icon(CupertinoIcons.profile_circled), title: Text('个人中心')),
  ];

  final List<Widget> _tabPages = [
    HomePage(),
    CategoryPage(),
    ShopCartPage(),
    MePage()
  ];

  int _currentTabIndex = 0;
  // Widget _currentPage;

  @override
  void initState() {
    // this._currentPage = this._tabPages[this._currentTabIndex];
    this._pageController = new PageController()
      ..addListener(() {
        if (this._currentTabIndex != this._pageController.page.round()) {
          this.setState(() {
            this._currentTabIndex = this._pageController.page.round();
          });
        }
      });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      body: IndexedStack(
        index: this._currentTabIndex,
        children: this._tabPages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: this._currentTabIndex,
        items: this._list,
        fixedColor: Colors.greenAccent,
        onTap: (index) {
          this.setState(() {
            this._currentTabIndex = index;
            // this._currentPage = this._tabPages[index];
          });
        },
      ),
    );
  }
}

// class IndexPage extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
// }
